// ==UserScript==
// @name		eArmy
// @version		2.0
// @description	eRepublik script that helps your being in the eArmy
// @author		Cryptos and Kallipos
// @namespace	cryptos
// @include		http://*erepublik.com/*
// @require		http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js
// ==/UserScript==

var LOCALE = 'en/';
var currURL = location.href;
var arrURL = currURL.split('/');
var BASE_URL = arrURL[0] + '/' + arrURL[1] + '/' + arrURL[2] + '/';
var ARMYSERVER = 'http://earmy.gotdns.org/eArmy/getOrdersNew/';
var ARMYIMAGESRC = 'http://earmy.gotdns.org/static/military_icon_normal.png';
var ARMYSERVERHOME = 'http://earmy.gotdns.org';
var ARMYREPORTURL = 'http://earmy.gotdns.org/eArmy/giveReport';
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
var is_opera = navigator.userAgent.toLowerCase().indexOf('opera') > -1;
var pid = 0;

function get_pid(){
	var $ = jQuery.noConflict();
	pid = $("div.backwhite a:first").attr("href").split("/").splice(-1)[0];
}

function inject_order(orderText){
    var $ = jQuery.noConflict();	
	var response = eval('(' + orderText + ')');
	var ordersList;
		
	if(response.constructor == Array)
		ordersList = response;
	else {
		ordersList = [];
		ordersList.push(response);
	}
		
	var obj = $("<div class='slideHolder' id='orders'><div style='font-size:20px; font-weight:strong'>Заповеди</div><br/></div>");
	var cont = $("div#content div.column:first").children(":first");
	obj.insertBefore(cont);
		
	ordersList.forEach(function(order) {
		var child = $("<div id='orders'></div>").attr("class", "art_elem");
		
		var image_holder = $("<div></div>").attr("class", "rankholder").css("background", "none");
		var image = $("<img/>").attr({src: 'http://earmy.gotdns.org/static/military_icon_normal.png', alt: 'military'});
		
		var order_holder = $("<div></div>").attr("class", "holder");
		
		$(image_holder).append(image);
		$(child).append(image_holder);
		$(child).append(order_holder);
		$(child).appendTo(obj);

		var order_html;
		switch(order.status) {
			case '0': 
				order_html = 'Не сте в армията. <h2><a href="' + ARMYSERVERHOME +'">Запиши се в армията</a></h2>';
				break;
			case '1': 
				order_html = 'Няма нови заповеди.';
				break;
			case '2': ;
				order_html = '<h2>Заповед: '+ order.from.split(" - ")[0] + ' <a href="' + order.target_link +'">'+ order.target + ' - ' + order.desc + '</a></h2>';
				break;
		}
		$(order_holder).html(order_html);
	});
}

function addOrdersChrome(){
	// for chrome support
	// makes json request to the server and ask for current profile_id's orders
	// downloads data as js and call the callback then modify DOM
	
	var script_src = '<script src="' + ARMYSERVER + pid + '.js" />';
	var $ = jQuery.noConflict();
	$('head').append(script_src);
}

function addOrders(){
	RetrieveOrders();
}

function RetrieveOrders(){

	GM_xmlhttpRequest({
		method: 'GET',
		url: ARMYSERVER + pid,
		onload: function (json) {inject_order(json.responseText);} });
}


function Main(e) {	
	get_pid();
	var subURL = currURL.substr(BASE_URL.length);
	BASE_URL += LOCALE;
	subURL = currURL.substr(BASE_URL.length);
	
	var $ = jQuery.noConflict();
	org = $("div.xprank").html().indexOf("Or") == 0;
	if (subURL == "" && !org) {
		//chrome and GM are acting differently when trying to do ajax stuff so separate processing is needed
		
		if ((!is_chrome) && (!is_opera)){
			addOrders();
		} else {
			addOrdersChrome();
		}
	}
}

jQuery(document).ready(function () {
    Main();
});
